package com.phiture.erp.produce.core.service.impl;

import com.phiture.erp.produce.core.service.ErpProduceTaskCommonService;
import com.phiture.erp.produce.dal.entity.ErpProduceMainPlanDO;
import com.phiture.erp.produce.dal.entity.ErpProductionTaskDO;
import com.phiture.erp.produce.dal.mapper.ErpProduceMainPlanMapper;
import com.phiture.erp.sale.api.pojo.resp.ErpSaleOrderItemResp;
import com.phiture.erp.sale.api.service.ErpSaleApi;
import org.springframework.stereotype.Service;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import javax.annotation.Resource;

@Service
public class ErpProduceTaskCommonServiceImpl implements ErpProduceTaskCommonService {

    @Resource
    private ErpProduceMainPlanMapper produceMainPlanMapper;

    @Resource
    private ErpSaleApi erpSaleApi;

    public void updateSaleOrderItemInQty(ErpProductionTaskDO task) {
        if (task != null) {
            ErpProduceMainPlanDO produceMainPlan = produceMainPlanMapper.selectByNo(task.getProductMainPlanNo());
            if (produceMainPlan == null) {
                return;
            }
            ErpSaleOrderItemResp saleOrderItem = erpSaleApi.getSaleOrderItemById(produceMainPlan.getSourceDetailId());
            if (saleOrderItem == null) {
                return;
            }
            saleOrderItem.setProduceInQty(task.getActualQuantity());
            erpSaleApi.updateSaleOrderItem(BeanUtils.toBean(saleOrderItem, ErpSaleOrderItemResp.class));
        }
    }
}
